<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\model\T_PM_MO_BASE;
use app\model\T_PM_MO_ONLINE;
use app\model\T_WIP_ERROR;
use app\model\YdayModoc;
use \think\facade\Db;
use think\facade\View;
use think\Request;
use think\route\dispatch\Controller;

class Kb extends BaseController
{
    /**
     * SMT看板服务
     */
    public function Smt(){
            //获取线体当前在制信息
            //字段
            $fields='a.AREA_SN,a.MO_DOC,a.MODEL_CODE,b.ITEM_SPEC,a.PLAN_QTY,c.INPUT_QTY,c.OUTPUT_QTY,a.ONLINE_TIME,c.PLAN_END_TIME';
            $res1=(new T_PM_MO_ONLINE())->alias('a')
                ->leftJoin('T_CO_ITEM b','a.MODEL_CODE=b.ITEM_CODE')
                ->leftJoin('T_PM_MO_BASE c','a.MO_DOC=c.MO_DOC')
                ->field($fields)
                ->where('a.AREA_SN','like','%SMT%')
                ->where('a.AREA_SN','<>','SMT1-7')
                ->select()->toarray();
            foreach ($res1 as $k=>$v){
                //数量取整
                $res1[$k]['PLAN_QTY']=intval($v['PLAN_QTY']);
                $res1[$k]['INPUT_QTY']=intval($v['INPUT_QTY']);
                $res1[$k]['OUTPUT_QTY']=intval($v['OUTPUT_QTY']);
                //计算SPI不良率，AOI良率,平均每小时产量
                //平均每小时产量=产出数量/（实际完工时间-实际开工时间），产出数量/（当前时间-实际开工时间) T_PM_MO_BASE（制令单基础信息表）
                $info=(new T_PM_MO_BASE())->where('MO_DOC',$v['MO_DOC'])->field('OUTPUT_QTY,ACTUAL_START_TIME,ACTUAL_END_TIME')->find();
                //有产出数量才计算不良率和良率
                if(intval($info['OUTPUT_QTY'])){
                    //判断是否有完工时间，无则按当前时间算
                    if($info['ACTUAL_END_TIME']){
                        $time=date('H',strtotime($info['ACTUAL_END_TIME'])-strtotime($info['ACTUAL_START_TIME']));
                        $res1[$k]['AvgNum']=intval($info['OUTPUT_QTY'])/intval($time);
                    }else{
                        $time=intval(date('H',time()-strtotime($info['ACTUAL_START_TIME'])));
                        if($time==0){
                            $res1[$k]['AvgNum']=intval($info['OUTPUT_QTY']);
                        }else{
                            $res1[$k]['AvgNum']=intval(intval($info['OUTPUT_QTY'])/$time);
                        }
                    }
                    //SPI不良率=不良品数量/产出数量 T_WIP_ERROR（不良信息表）
                    $info2=(new T_WIP_ERROR())->where('MO_NUMBER',$v['MO_DOC'])->where('CREATE_BY','=','SPI')->field('NG_QTY')->find();
                    if($info2){
                        $num=intval($info2['NG_QTY'])/intval($info['OUTPUT_QTY']);
                        $SPI=(substr((string)$num,0,5));
                        $res1[$k]['SPI']=$SPI.'%';
                    }else{
                        $res1[$k]['SPI']='/';
                    }
                    //计算AOI良率=100-（不良品数量/产出数量） T_WIP_ERROR（不良信息表）
                    $info3=(new T_WIP_ERROR())->where('MO_NUMBER',$v['MO_DOC'])->where('CREATE_BY','=','AOI')->field('NG_QTY')->find();
                    if($info3){
                        $num=100-(intval($info2['NG_QTY'])/intval($info['OUTPUT_QTY']));
                        $AOI=(substr((string)$num,0,5));
                        $res1[$k]['AOI']=$AOI.'%';
                    }else{
                        $res1[$k]['AOI']='/';
                    }
                }else{
                    $res1[$k]['AvgNum']=0;
                    $res1[$k]['AOI']='/';
                    $res1[$k]['SPI']='/';
                }
                //预计完工时间为空
                if(empty($v['PLAN_END_TIME'])){
                    $res1[$k]['PLAN_END_TIME']='/';
                }
                //制令单上线时间为空
                if(empty($v['ONLINE_TIME'])){
                    $res1[$k]['ONLINE_TIME']='/';
                }

            }
            //获取待上线制令单信息
            $fields2='a.MO_DOC,b.ITEM_NAME,a.PLAN_START_TIME';
            $res2=(new T_PM_MO_BASE())->alias('a')
                ->leftJoin('T_CO_ITEM b','a.MODEL_CODE=b.ITEM_CODE')
                ->field($fields2)
                ->where('a.PRODUCT_STEP','like','%SMT%')
                ->where('STATUS','=',1)
                ->select()->toarray();
            $res2['data']=$res2;
            $res2['count']=count($res2);
            //获取昨日线体制令单生产信息
            $creat_time[]=['create_time','>=',date('Y-m-d 00:00:00',strtotime("-1 day"))];
            $creat_time[]=['create_time','<=',date('Y-m-d 23:59:59',strtotime("-1 day"))];
            $res3=(new YdayModoc())->where('type',1)->where($creat_time)->select()->toArray();
            //昨日在制制令单数量
            $count=count($res3);
            //昨日生产数量总和
            $sum=0;
            //整体不良率
            $NGALL=0;
            foreach ($res3 as $k=>$v){
                $sum+=$v['OUTPUT_QTY'];
                $NGALL+=$v['NG'];
            }
        if($this->request->isAjax()){
             return json(['data'=>$res1,'data2'=>$res2['data'],'data3'=>$res3,'count'=>$count,'sum'=>intval($sum),'buHao'=>$NGALL],200);
        }else{
            View::assign('res2',json_encode($res2));
            return view('/SMT',['data'=>$res1,'data2'=>$res2,'data3'=>$res3,'count'=>$count,'sum'=>intval($sum),'buHao'=>$NGALL]);
        }

    }

    /**
     * DIP看板服务
     */
    public function Dip(){
        //获取线体当前在制信息
        //字段
        $fields='a.AREA_SN,a.MO_DOC,a.MODEL_CODE,b.ITEM_SPEC,a.PLAN_QTY,c.INPUT_QTY,c.OUTPUT_QTY,a.ONLINE_TIME,c.PLAN_END_TIME';
        $res1=(new T_PM_MO_ONLINE())->alias('a')
            ->leftJoin('T_CO_ITEM b','a.MODEL_CODE=b.ITEM_CODE')
            ->leftJoin('T_PM_MO_BASE c','a.MO_DOC=c.MO_DOC')
            ->field($fields)
            ->where('a.AREA_SN','like','%DIP%')
            ->select()->toarray();

        foreach ($res1 as $k=>$v){
            //数量取整
            $res1[$k]['PLAN_QTY']=intval($v['PLAN_QTY']);
            $res1[$k]['INPUT_QTY']=intval($v['INPUT_QTY']);
            $res1[$k]['OUTPUT_QTY']=intval($v['OUTPUT_QTY']);
            //计算SPI不良率，AOI良率,平均每小时产量
            //平均每小时产量=产出数量/（实际完工时间-实际开工时间），产出数量/（当前时间-实际开工时间) T_PM_MO_BASE（制令单基础信息表）
            $info=(new T_PM_MO_BASE())->where('MO_DOC',$v['MO_DOC'])->field('OUTPUT_QTY,ACTUAL_START_TIME,ACTUAL_END_TIME')->find();
            //有产出数量才计算不良率和良率
            if(intval($info['OUTPUT_QTY'])){
                //判断是否有完工时间，无则按当前时间算
                if($info['ACTUAL_END_TIME']){
                    $time=date('H',strtotime($info['ACTUAL_END_TIME'])-strtotime($info['ACTUAL_START_TIME']));
                    $res1[$k]['AvgNum']=intval($info['OUTPUT_QTY'])/intval($time);
                }else{
                    $time=intval(date('H',time()-strtotime($info['ACTUAL_START_TIME'])));
//                    dump(time());dump($info['ACTUAL_START_TIME']);dump(strtotime($info['ACTUAL_START_TIME']));dump($time);dump(intval($info['OUTPUT_QTY']));
                    if($time==0){
                        $res1[$k]['AvgNum']=intval($info['OUTPUT_QTY']);
                    }else{
                        $res1[$k]['AvgNum']=intval(intval($info['OUTPUT_QTY'])/$time);
                    }
                }
            }else{
                $res1[$k]['AvgNum']=0;
            }
            //预计完工时间为空
            if(empty($v['PLAN_END_TIME'])){
                $res1[$k]['PLAN_END_TIME']='/';
            }
            //制令单上线时间为空
            if(empty($v['ONLINE_TIME'])){
                $res1[$k]['ONLINE_TIME']='/';
            }

        }
        //获取待上线制令单信息
        $fields2='a.MO_DOC,b.ITEM_NAME,a.PLAN_START_TIME';
        $res2=(new T_PM_MO_BASE())->alias('a')
            ->leftJoin('T_CO_ITEM b','a.MODEL_CODE=b.ITEM_CODE')
            ->field($fields2)
            ->where('a.PRODUCT_STEP','like','%THT%')
            ->where('STATUS','=',1)
            ->select()->toarray();
        $res2['data']=$res2;
        $res2['count']=count($res2);
        //获取昨日线体制令单生产信息
        $creat_time[]=['create_time','>=',date('Y-m-d 00:00:00',strtotime("-1 day"))];
        $creat_time[]=['create_time','<=',date('Y-m-d 23:59:59',strtotime("-1 day"))];
        $res3=(new YdayModoc())->where('type',2)->where($creat_time) ->select()->toArray();
        //昨日在制制令单数量
        $count=count($res3);
        //昨日生产数量总和
        $sum=0;
        //整体不良率
        $NGALL=0;
        foreach ($res3 as $k=>$v){
            $sum+=$v['OUTPUT_QTY'];
            $NGALL+=$v['NG'];
        }
        if($this->request->isAjax()){
            return json(['data'=>$res1,'data2'=>$res2['data'],'data3'=>$res3,'count'=>$count,'sum'=>intval($sum),'buHao'=>$NGALL],200);
        }else{
            View::assign('res2',json_encode($res2));
            return view('/DIP',['data'=>$res1,'data2'=>$res2,'data3'=>$res3,'count'=>$count,'sum'=>intval($sum),'buHao'=>$NGALL]);
        }
    }

    /**
     * 总装看板服务
     */
    public function Zz(){
        //获取线体当前在制信息
        //字段
        $fields='a.AREA_SN,a.MO_DOC,a.MODEL_CODE,b.ITEM_SPEC,a.PLAN_QTY,c.INPUT_QTY,c.OUTPUT_QTY,a.ONLINE_TIME,c.PLAN_END_TIME';
        $res1=(new T_PM_MO_ONLINE())->alias('a')
            ->leftJoin('T_CO_ITEM b','a.MODEL_CODE=b.ITEM_CODE')
            ->leftJoin('T_PM_MO_BASE c','a.MO_DOC=c.MO_DOC')
            ->field($fields)
            ->where('a.AREA_SN','like','%AST/PCK%')
            ->select()->toarray();

        foreach ($res1 as $k=>$v){
            //数量取整
            $res1[$k]['PLAN_QTY']=intval($v['PLAN_QTY']);
            $res1[$k]['INPUT_QTY']=intval($v['INPUT_QTY']);
            $res1[$k]['OUTPUT_QTY']=intval($v['OUTPUT_QTY']);
//            //计算SPI不良率，AOI良率,平均每小时产量
//            //平均每小时产量=产出数量/（实际完工时间-实际开工时间），产出数量/（当前时间-实际开工时间) T_PM_MO_BASE（制令单基础信息表）
            $info=(new T_PM_MO_BASE())->where('MO_DOC',$v['MO_DOC'])->field('OUTPUT_QTY,ACTUAL_START_TIME,ACTUAL_END_TIME')->find();
            //有产出数量才计算不良率和良率
            if(intval($info['OUTPUT_QTY'])){
                //判断是否有完工时间，无则按当前时间算
                if($info['ACTUAL_END_TIME']){
                    $time=date('H',strtotime($info['ACTUAL_END_TIME'])-strtotime($info['ACTUAL_START_TIME']));
                    $res1[$k]['AvgNum']=intval($info['OUTPUT_QTY'])/intval($time);
                }else{
                    $time=intval(date('H',time()-strtotime($info['ACTUAL_START_TIME'])));
                    if($time==0){
                        $res1[$k]['AvgNum']=intval($info['OUTPUT_QTY']);
                    }else{
                        $res1[$k]['AvgNum']=intval(intval($info['OUTPUT_QTY'])/$time);
                    }

                }
            }else{
                $res1[$k]['AvgNum']=0;
            }
            //预计完工时间为空
            if(empty($v['PLAN_END_TIME'])){
                $res1[$k]['PLAN_END_TIME']='/';
            }
            //制令单上线时间为空
            if(empty($v['ONLINE_TIME'])){
                $res1[$k]['ONLINE_TIME']='/';
            }

        }
        //获取待上线制令单信息
        $fields2='a.MO_DOC,b.ITEM_NAME,a.PLAN_START_TIME';
        $res2=(new T_PM_MO_BASE())->alias('a')
            ->leftJoin('T_CO_ITEM b','a.MODEL_CODE=b.ITEM_CODE')
            ->field($fields2)
            ->where('a.PRODUCT_STEP','like','%总装%')
            ->where('STATUS','=',1)
            ->select()->toarray();
        $res2['data']=$res2;
        $res2['count']=count($res2);
        //获取昨日线体制令单生产信息
        $creat_time[]=['create_time','>=',date('Y-m-d 00:00:00',strtotime("-1 day"))];
        $creat_time[]=['create_time','<=',date('Y-m-d 23:59:59',strtotime("-1 day"))];
        $res3=(new YdayModoc())->where('type',3)
            ->where($creat_time)
            ->select()->toArray();
        //昨日在制制令单数量
        $count=count($res3);
        //昨日生产数量总和
        $sum=0;
        //整体不良率
        $NGALL=0;
        foreach ($res3 as $k=>$v){
            $sum+=$v['OUTPUT_QTY'];
            $NGALL+=$v['NG'];
        }
        if($this->request->isAjax()){
            return json(['data'=>$res1,'data2'=>$res2['data'],'data3'=>$res3,'count'=>$count,'sum'=>intval($sum),'buHao'=>$NGALL],200);
        }else{
            View::assign('res2',json_encode($res2));
            return view('/ZZ',['data'=>$res1,'data2'=>$res2,'data3'=>$res3,'count'=>$count,'sum'=>intval($sum),'buHao'=>$NGALL]);
        }
    }

}
